// pages/meinv_detail/meinv_detail.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    datas:{},
    url:'https://hl.gzziyu.com/',
    eva_list:{},
    eva_text: ''
  },
  inputchange:function(e){
    this.setData({
      eva_text:e.detail.value
    })
  },
  go_jingcai:function(){
    wx.switchTab({
      url: '../cai/cai'
    })
  },
  dianzan:function(e){
    var index = e.currentTarget.dataset.index;
    var catid = e.currentTarget.dataset.catid;
    var pid = e.currentTarget.dataset.pid;
    var that = this;
    console.log(index,catid,pid)
    wx.request({
      url: app.globalData.url+'ziyu.php?Action=dianzan',
      data:{
        openid:app.globalData.opid,
        articleid: catid,
        pid: pid
        
      },
      success:function(res){
        console.log(res)
        if (res.data.success == 1){
         wx.showToast({
           title: '点赞成功',
           icon: 'success'
         })
          wx.request({
            url: app.globalData.url + 'ziyu.php?Action=pinglunlist',
            data: {
              openid: app.globalData.opid,
              articleid: that.data.datas.article_id
            },
            success: function (res) {
              console.log(res)
              that.setData({
                eva_list: res.data
              })
            }
          })
        } else if (res.data.success == 2) {
         wx.showToast({
           title: '您已点赞'
         })
       } else {
         wx.showToast({
           title: '非法操作'
         })
       }
        

      }
    })
  },
  // 去商品详情
  go_shop_detail: function (e) {
    console.log(e.currentTarget.dataset.id)
    wx.navigateTo({
      url: '../product_detail/product_detail?id=' + e.currentTarget.dataset.id,
    })
  },
  get_eva: function (e) {
    var that = this;
    console.log(e)
    console.log(e.target.dataset.id, app.globalData.opid, that.data.eva_text)
    wx.request({
      url: app.globalData.url+'ziyu.php?Action=pingluntj',
      data: {
        articleid: e.target.dataset.id,
        openid: app.globalData.opid,
        content: that.data.eva_text
      },
      success: function (res) {
        console.log(res)
       if(res.data.success == 1){
         wx.showToast({
           title: '评论成功',
           icon: 'success'
         })
         wx.request({
           url: app.globalData.url + 'ziyu.php?Action=pinglunlist',
           data: {
             openid: app.globalData.opid,
             articleid: that.data.datas.article_id
           },
           success: function (res) {
             console.log(res)
             that.setData({
               eva_list: res.data
             })
           }
         })
         that.setData({
           eva_text: ''
         })
       }

      }
    })
  },
  goshangxia:function(e){
    if(e.currentTarget.dataset.id){
      wx.navigateTo({
        url: '../meinv_detail/meinv_detail?id=' + e.currentTarget.dataset.id,
        
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  

  onLoad: function (options) {
    var that = this;
    var id = options.id;
    that.setData({
      new_id: id
    })
    wx.showLoading({
      title: '加载中',
    })
    console.log("meinv id :"+id)
    // 推荐
    wx.request({
      url: app.globalData.url + 'ziyu.php?Action=product',
      success: function (res) {
        console.log('商城')
        console.log(res)
       
        console.log(res)
        that.setData({
          shop_list: res.data
        })
        wx.hideLoading()
      }
    })
    wx.request({
      url: app.globalData.url + 'ziyu.php?Action=article&id=' + id,
      success: function (res) {
        console.log(res)
        let content = res.data.content;
        res.data.content = content.replace(/\<img/gi, '<img style="width:100% !important;height:auto!important" ')
        that.setData({
            datas:res.data
        })

        wx.request({
          url: app.globalData.url +'ziyu.php?Action=pinglunlist',
          data:{
            openid:app.globalData.opid,
            articleid: res.data.article_id
          },
          success:function(res){
            console.log(res)
            wx.hideLoading()
            that.setData({
              eva_list:res.data
            })
          }
        })
      }
    })
    wx.request({
      url: app.globalData.url+'/ziyu.php?Action=pnext',
      data:{
        id:id
      },
      success:function(res){
        console.log(res)
        that.setData({
          next_prve: res.data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this;
    console.log(that.data.datas)
    return {
      title: that.data.datas.title,
      path: 'pages/meinv_detail/meinv_detail?id=' + that.data.datas.article_id,
      imageUrl: "https://hl.gzziyu.com/" + that.data.datas.ewmfile
    }
  }
})